/**
 * @classDescription  文字滚动构造函数
 * @type {Object}
 * @param {String | Object} 滚动层的id或者Dom对象 
 * @param {String | Object} 克隆层的id或者Dom对象 
 * @param {Object}  参数集合（包括dir, pause, timeout, speed, stopMoving, stopSize, step, callback）
 */ 
function Scroll(id1, id2, option) {
	this.setOption(option);
    this.ori = Kg.$(id1);
    this.clone = Kg.$(id2);
    this.father = this.ori.parentNode;
    this.timer = null;
    this.clone.innerHTML = this.ori.innerHTML;
    this.father = (this.option.dir === "Left" || this.option.dir === "Right") ? 
		this.father.parentNode: 
		this.father; 
	
	!this.option.hand && setTimeout(Kg.bind(function() {
        this.run();
    },this), 0); 
	
	! this.option.hand && this.option.stopMoving && Kg.addEvent(this.father, "mouseover", Kg.bind(function() {
        this.stop();
    },this)); 
	
	! this.option.hand && this.option.stopMoving && Kg.addEvent(this.father, "mouseout", Kg.bind(function() {
        this.run();
    },this));
};
Scroll.prototype.setOption = function(option) {
    this.option = {
        hand: false,
        dir: "Down",
        pause: false,
        timeout: 1000,
        speed: 30,
		stopMoving: true,
        stopSize: 50,
        step: 1,
		callback: null
    };
    Kg.extend(this.option, option || {}, true);
};
Scroll.prototype.run = function() {
    switch (this.option.dir) {
    case "Up":
    case "Down":
        this.size = this.ori.offsetHeight;
        (this.size >= this.father.offsetHeight) && this["scroll" + this.option.dir]();
        break;
    case "Left":
    case "Right":
        this.size = this.ori.offsetWidth;
        this["scroll" + this.option.dir]();
        break;
    }
};
Scroll.prototype.scrollLeft = function() {
	if(this.option.hand && this.working){
		 return;
	}
	this.working = true;
    this.stop();
	this.sL = this.father.scrollLeft;
	var n = 0;
    this.timer = setInterval(Kg.bind(function() {
        if(this.sL <= 0){
			this.sL = this.father.scrollLeft = this.size;	
		} else {
			this.sL = this.father.scrollLeft = this.sL - this.option.step;
			n += this.option.step;
		}
	
		if(this.option.pause && n >= this.option.stopSize){
			clearInterval(this.timer); 
			this.working = false;
			this.option.callback && this.option.callback();
			n = 0;
			! this.option.hand && setTimeout(Kg.bind(this.run, this), this.option.timeout);	
		}
    },this), this.option.speed);
};
Scroll.prototype.scrollRight = function() {
	if(this.option.hand && this.working){
		 return;
	}
	this.working = true;
    this.stop();
	this.sL = this.father.scrollLeft;
	var n = 0;
    this.timer = setInterval(Kg.bind(function() {
        if(this.sL >= this.size){
			this.sL = this.father.scrollLeft = 0 ;
		} else {
			this.sL = this.father.scrollLeft = this.sL + this.option.step;
			n += this.option.step;
		}
		
		if(this.option.pause && n >= this.option.stopSize){
			clearInterval(this.timer); 
			this.working = false;
			this.option.callback && this.option.callback();
			n = 0;
			! this.option.hand && setTimeout(Kg.bind(this.run, this), this.option.timeout);
		}
    },this), this.option.speed);
};
Scroll.prototype.scrollUp = function() {
	if(this.option.hand && this.working){
		 return;
	}
	this.working = true;
    this.stop();
	this.sT = this.father.scrollTop;
	var n = 0;
    this.timer = setInterval(Kg.bind(function() {
        if(this.sT <= 0){
			this.sT = this.father.scrollTop = this.size;
		} else {
			this.sT = this.father.scrollTop = this.sT - this.option.step;
			n += this.option.step;
		}
			 		
        if (this.option.pause && n >= this.option.stopSize) {
			clearInterval(this.timer); 
			this.working = false;
			this.option.callback && this.option.callback();
			n = 0;
			! this.option.hand && setTimeout(Kg.bind(this.run, this), this.option.timeout);
        }
    },this), this.option.speed);
};
Scroll.prototype.scrollDown = function() {
	if(this.option.hand && this.working){
		 return;
	}
	this.working = true;
    this.stop();
	this.sT = this.father.scrollTop;
	var n = 0;
    this.timer = setInterval(Kg.bind(function() {
        if(this.sT >= this.size){
			this.sT = this.father.scrollTop = 0
		} else {
			this.sT = this.father.scrollTop = this.sT + this.option.step;
			n += this.option.step;
		}
		
        if (this.option.pause && n >= this.option.stopSize) {
			clearInterval(this.timer); 
			this.working = false;
			this.option.callback && this.option.callback();
			n = 0;
			! this.option.hand && setTimeout(Kg.bind(this.run, this), this.option.timeout);
        }
    },this), this.option.speed);
};
Scroll.prototype.stop = function() {
    this.timer && clearInterval(this.timer);
};
Scroll.prototype.restart = function() {
    this.clone.innerHTML = this.ori.innerHTML;
    this.run();
}